﻿$().ready(function () {
    $('.ititle,.il,.ir').ifixpng();
    $('input[name=VerifyCode],#VerifyImg').click(function () {
        ShowVerifyimg('login', 'VerifyImg', 'VerifyCode');
    });
    $('input[name=VerifyCode]').focus(function () {
        ShowVerifyimg('login', 'VerifyImg', 'VerifyCode');
    });
    $('#form1').validate(
	{
	    rules:
		{
		    UserName: { required: true },
		    UserPassword: { required: true },
		    VerifyCode: { required: true }
		},
	    messages: {
	        UserName: '*',
	        UserPassword: '*',
	        VerifyCode: '*'
	    },
	    submitHandler: function (form) {
	        $('.msg').html('正在登录中...');
	        $(form).ajaxSubmit({ url: 'ajax/login.aspx',
	            dataType: 'json',
	            cache: false,
	            success: function (data) {
	                $('.msg').html(data.Message);
	                if (data.Code == 200) {
	                    if (data.ItemID == 1) {
	                        //document.location.href = '/';
	                        document.location.href = 'main.aspx';
	                        //document.location.href = 'agent/client-list.aspx';   //代理商
	                    }
	                    else if (data.ItemID == 2)
	                        document.location.href = 'select.html';
	                    else {
	                        //document.location.href = '/my.shtml';
	                        document.location.href = 'main.aspx';
	                    }
	                }
	            }
	        });
	    }
	});
});


function ShowVerifyimg(sn, img, input, form) {
    if (typeof (form) == "string") form = document.getElementById(form);
    if (typeof (img) == "string") {
        if (form) {
            var tmp = form.getElementsByTagName("SPAN");
            for (var i = 0; i < tmp.length; i++) {
                if (tmp[i].id == img) img = tmp[i];
            }
        }
        else {
            img = document.getElementById(img);
        }
    }
    img.innerHTML = "<img src=\"ajax/VerifyImage.aspx?sb=1&st=1&w=55&h=20&fs=12&key=" + sn + "&" + Math.random() + "\" alt=\"\" width=\"55\" height=\"20\" align=\"absMiddle\" style=\"cursor: pointer;\" />";

    $('input[name=' + input + ']').unbind('click').unbind('focus');
};

(function ($) {

    /**
    * helper variables and function
    */
    $.ifixpng = function (customPixel) {
        $.ifixpng.pixel = customPixel;
    };

    $.ifixpng.getPixel = function () {
        return $.ifixpng.pixel || 'images/pixel.gif';
    };

    var hack = {
        ltie7: $.browser.msie && $.browser.version < 7,
        filter: function (src) {
            return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='" + src + "')";
        }
    };

    /**
    * Applies ie png hack to selected dom elements
    *
    * $('img[@src$=.png]').ifixpng();
    * @desc apply hack to all images with png extensions
    *
    * $('#panel, img[@src$=.png]').ifixpng();
    * @desc apply hack to element #panel and all images with png extensions
    *
    * @name ifixpng
    */

    $.fn.ifixpng = hack.ltie7 ? function () {
        return this.each(function () {
            var $$ = $(this);
            // in case rewriting urls
            var base = $('base').attr('href');
            if (base) {
                // remove anything after the last '/'
                base = base.replace(/\/[^\/]+$/, '/');
            }
            if ($$.is('img') || $$.is('input:image')) { // hack image tags present in dom
                if ($$.attr('src')) {
                    if ($$.attr('src').match(/.*\.png([?].*)?$/i)) { // make sure it is png image
                        // use source tag value if set 
                        var source = (base && $$.attr('src').search(/^(\/|http:)/i)) ? base + $$.attr('src') : $$.attr('src');
                        // apply filter
                        $$.css({ filter: hack.filter(source), width: $$.width(), height: $$.height() })
						  .attr({ src: $.ifixpng.getPixel() })
						  .positionFix();
                    }
                }
            } else { // hack png css properties present inside css
                var image = $$.css('backgroundImage');
                if (image.match(/^url\(["']?(.*\.png([?].*)?)["']?\)$/i)) {
                    image = RegExp.$1;
                    image = (base && image.substring(0, 1) != '/') ? base + image : image;
                    $$.css({ backgroundImage: 'none', filter: hack.filter(image) })
					  .children().children().positionFix();
                }
            }
        });
    } : function () { return this; };

    /**
    * Removes any png hack that may have been applied previously
    *
    * $('img[@src$=.png]').iunfixpng();
    * @desc revert hack on all images with png extensions
    *
    * $('#panel, img[@src$=.png]').iunfixpng();
    * @desc revert hack on element #panel and all images with png extensions
    *
    * @name iunfixpng
    */

    $.fn.iunfixpng = hack.ltie7 ? function () {
        return this.each(function () {
            var $$ = $(this);
            var src = $$.css('filter');
            if (src.match(/src=["']?(.*\.png([?].*)?)["']?/i)) { // get img source from filter
                src = RegExp.$1;
                if ($$.is('img') || $$.is('input')) {
                    $$.attr({ src: src }).css({ filter: '' });
                } else {
                    $$.css({ filter: '', background: 'url(' + src + ')' });
                }
            }
        });
    } : function () { return this; };

    /**
    * positions selected item relatively
    */

    $.fn.positionFix = function () {
        return this.each(function () {
            var $$ = $(this);
            var position = $$.css('position');
            if (position != 'absolute' && position != 'relative') {
                $$.css({ position: 'relative' });
            }
        });
    };

})(jQuery);